﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NailManager.DTO;
using System.Data.OleDb;
using System.Collections;
using System.Data;

namespace NailManager.DAO
{
    public class AccountDAO : DataProvider
    {
        public ArrayList getList()
        {
            connect();
            ArrayList arr;
            try
            {
                string query = "SELECT * FROM ACCOUNT";
                adapter = new OleDbDataAdapter(query, connection);
                DataSet dataset = new DataSet();
                adapter.Fill(dataset);
                arr = ConvertDataSetToArrayList(dataset);
                
            }
            catch (Exception ex)
            {
                arr = new ArrayList();
            }
            return arr;
        }

        public ArrayList getList(string condition)
        {
            connect();
            ArrayList arr;
            try
            {
                string query = "SELECT * FROM ACCOUNT" + condition;
                adapter = new OleDbDataAdapter(query, connection);
                DataSet dataset = new DataSet();
                adapter.Fill(dataset);
                arr = ConvertDataSetToArrayList(dataset);

            }
            catch (Exception ex)
            {
                arr = new ArrayList();
            }
            return arr;
        }

        public void insert(Account info)
        {
            try
            {
                string insertCommand = "";
                OleDbParameter parameter = new OleDbParameter("@UserID", OleDbType.VarChar);
                parameter.Value = info.UserID;
                command.Parameters.Add(parameter);

                parameter = new OleDbParameter("@DateOfBirth", OleDbType.DBDate);
                parameter.Value = info.DateOfBirth;
                command.Parameters.Add(parameter);

                parameter = new OleDbParameter("@Email", OleDbType.VarChar);
                parameter.Value = info.Email;
                command.Parameters.Add(parameter);

                parameter = new OleDbParameter("@FirstName", OleDbType.VarChar);
                parameter.Value = info.FirstName;
                command.Parameters.Add(parameter);

                parameter = new OleDbParameter("@HashPassWord", OleDbType.VarChar);
                parameter.Value = info.HashPassWord;
                command.Parameters.Add(parameter);

                parameter = new OleDbParameter("@LastName", OleDbType.VarChar);
                parameter.Value = info.LastName;
                command.Parameters.Add(parameter);

                parameter = new OleDbParameter("@PhoneNumber", OleDbType.VarChar);
                parameter.Value = info.PhoneNumber;
                command.Parameters.Add(parameter);

                parameter = new OleDbParameter("@UserAddress", OleDbType.VarChar);
                parameter.Value = info.UserAddress;
                command.Parameters.Add(parameter);

                parameter = new OleDbParameter("@UserPermisstion", OleDbType.Integer);
                parameter.Value = info.UserPermisstion;
                command.Parameters.Add(parameter);

                executeNonQuery(insertCommand);
                command.Parameters.Clear();
            }
            catch (Exception ex)
            {
                command.Parameters.Clear();
                return;
            }
        }

        public void update(Account info)
        {
            try
            {
                string updateCommand = "";
                OleDbParameter parameter = new OleDbParameter("@UserID", OleDbType.VarChar);
                parameter.Value = info.UserID;
                command.Parameters.Add(parameter); 
                
                parameter = new OleDbParameter("@DateOfBirth", OleDbType.DBDate);
                parameter.Value = info.DateOfBirth;
                command.Parameters.Add(parameter);

                parameter = new OleDbParameter("@Email", OleDbType.VarChar);
                parameter.Value = info.Email;
                command.Parameters.Add(parameter);

                parameter = new OleDbParameter("@FirstName", OleDbType.VarChar);
                parameter.Value = info.FirstName;
                command.Parameters.Add(parameter);

                parameter = new OleDbParameter("@HashPassWord", OleDbType.VarChar);
                parameter.Value = info.HashPassWord;
                command.Parameters.Add(parameter);

                parameter = new OleDbParameter("@LastName", OleDbType.VarChar);
                parameter.Value = info.LastName;
                command.Parameters.Add(parameter);

                parameter = new OleDbParameter("@PhoneNumber", OleDbType.VarChar);
                parameter.Value = info.PhoneNumber;
                command.Parameters.Add(parameter);

                parameter = new OleDbParameter("@UserAddress", OleDbType.VarChar);
                parameter.Value = info.UserAddress;
                command.Parameters.Add(parameter);

                parameter = new OleDbParameter("@UserPermisstion", OleDbType.Integer);
                parameter.Value = info.UserPermisstion;
                command.Parameters.Add(parameter);

                executeNonQuery(updateCommand);
                command.Parameters.Clear();
            }
            catch (Exception ex)
            {
                command.Parameters.Clear();
                return;
            }
        }

        public void delete(string id)
        {
            try
            {
                string deleteCommand = "";
                OleDbParameter parameter = new OleDbParameter("@UserID", OleDbType.VarChar);
                parameter.Value = id;
                command.Parameters.Add(parameter);
                executeNonQuery(deleteCommand);
                command.Parameters.Clear();
            }
            catch (Exception ex)
            {
                command.Parameters.Clear();
                return;
            }
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            Account info = new Account();
            info.UserID = dt.Rows[i]["UserID"].ToString();
            info.FirstName = dt.Rows[i]["FirstNameD"].ToString();
            info.LastName = dt.Rows[i]["LastName"].ToString();
            info.DateOfBirth = DateTime.Parse(dt.Rows[i]["DateOfBirth"].ToString());
            info.Email = dt.Rows[i]["Email"].ToString();
            info.PhoneNumber = dt.Rows[i]["PhoneNumber"].ToString();
            info.UserAddress = dt.Rows[i]["UserAddress"].ToString();
            info.UserPermisstion = int.Parse(dt.Rows[i]["UserPermisstion"].ToString()); 

            return (object)info;
        }
    }
}
